package com.zwz.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


@Slf4j
@Aspect
@Component
public class DaoMonitorAspect {
//    @Autowired
//    private AlertService alertService;
//


    @AfterThrowing(pointcut = "execution(* com.zwz.mapper.*.*(..))",
            throwing = "ex")
    public void logDaoException(JoinPoint joinPoint, DuplicateKeyException ex) {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String errorDetail = String.format("DAO层异常 - %s.%s: %s", className, methodName, ex.getMessage());

        // 发送告警
//        alertService.sendAlert("DAO层主键冲突", errorDetail);

        //统一记录到DB中,数据库后台设置了唯一约束，业务数据是正确的。先放到数据库中了

        log.error(errorDetail);


    }





}

